package edu.calpoly.android.echoes;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;

public class PauseOverlay {

    private int width, height;
    private float centerX, centerY;
    private RectF pauseBarL, pauseBarR;
    private RectF toMain, toSave;
    private Path sideTri;
    private Paint pauseColor;
    private Paint backColor;
    private Paint selectedColor;
    
    public static final int PAUSE_LEFT = 0, PAUSE_START = 1, PAUSE_RIGHT = 2, PAUSE_QUIT = 3, PAUSE_SAVE = 4;
    
    //background left arrow, pause button, and right arrow
    private RectF bgLA, bgPB, bgRA;
	
    
	public PauseOverlay() {
		width = 0;
		height = 0;
		centerX = 0;
		centerY = 0;
	}
	
	
	public void drawMe(Canvas c) {
		if(c.getWidth() != width || c.getHeight() != height) {
			calculateDimensionsAndShapes(c);
		}
		
		drawPauseSymbol(c);
		drawSideArrows(c);
		drawMenuButtons(c);
	}
	
	private void calculateDimensionsAndShapes(Canvas c) {
		width = c.getWidth();
		height = c.getHeight();
		centerX = c.getWidth()/2;
		centerY = c.getHeight()/2;

		setColors();
		setPauseBars();
		setTriangles();
		setBackgrounds();
		setMenuButtons();
	}
	
	private void setColors() {
		pauseColor = new Paint(Color.BLACK);
		pauseColor.setStyle(Paint.Style.FILL);
		pauseColor.setStrokeWidth(2);
		pauseColor.setAntiAlias(true);
		
		backColor = new Paint(Color.rgb(169,229,255));
		backColor.setStyle(Paint.Style.FILL);
		backColor.setAntiAlias(true);
		
		selectedColor = new Paint(Color.rgb(255,204,51));
		selectedColor.setStyle(Paint.Style.FILL);
		selectedColor.setStrokeWidth(2);
		selectedColor.setAntiAlias(true);
	}
	
	private void setPauseBars() {
		pauseBarL = new RectF((float)(centerX - (width/8) + (width/20)), (float)(centerY - (height/5)),(float)(centerX - (width/12) + (width/20)), (float)(centerY));
		pauseBarR = new RectF((float)(centerX + (width/12) - (width/20)), (float)(centerY - (height/5)),(float)(centerX + (width/8) - (width/20)), (float)(centerY));		
	}
	
	private void setTriangles() {
		sideTri = new Path();
		
		sideTri.setFillType(Path.FillType.EVEN_ODD);

		sideTri.moveTo(width/20 + width/8, height/10);
		sideTri.lineTo(width/20, 0);
		sideTri.lineTo(width/20 + width/8, -height/10);
		sideTri.close();

		sideTri.offset(width/20,centerY - (height/10));
	}
	
	private void setBackgrounds() {
		bgLA = new RectF((float)(0),
				         (float)(centerY - (height/5) - (height/15)),
				         (float)(0 + (width/8) + (width/20) + (width/15)),
				         (float)(centerY + (height/15)));
		bgPB = new RectF((float)(centerX - (width/8) + (width/20) - (width/15)),
				         (float)(centerY - (height/5) - (height/15)),
				         (float)(centerX + (width/8) - (width/20) + (width/15)),
				         (float)(centerY + (height/15) ));
		bgRA = new RectF((float)(width - (width/20) - (width/8) - (width/15)),
					     (float)(centerY - (height/5) - (height/15)),
				         (float)(width),
				         (float)(centerY + (height/15)));
	}
	
	private void setMenuButtons() {
		toMain = new RectF((float)(0),
				           (float)(height - (height/10)),
				           (float)(centerX),
				           (float)(height));
		toSave = new RectF((float)(centerX),
						   (float)(height - (height/10)),
				           (float)(width),
				           (float)(height));
	}
	
	private void drawSideArrows(Canvas c) {
		c.drawPath(sideTri, pauseColor);
		c.rotate(180, centerX, centerY - (height/10));
		c.drawPath(sideTri, pauseColor);
		c.restore();
	}
	
	public void drawPauseSymbol(Canvas c) {
		c.drawRect(pauseBarL, pauseColor);
		c.drawRect(pauseBarR, pauseColor);		
	}
	
	public void drawMenuButtons(Canvas c) {
		Paint p = new Paint(Color.RED);
		p.setStyle(Paint.Style.FILL);
		p.setStrokeWidth(5);
		p.setAntiAlias(true);
		
		c.drawRect(toMain, p);
		
		p.setColor(Color.rgb(255,130,0));
		c.drawRect(toSave, p);

		p.setTextSize(width/12);
		p.setStrokeWidth(1);
		p.setColor(Color.WHITE);
		
		//c.drawText("Main Menu", centerX/15, height - (height/30), p);
		c.drawText("Save Game", centerX + centerX/15, height - (height/30), p);
	}
	
	public int checkCollision(int x, int y) {
		if(bgLA.contains(x,y)) {
			return PAUSE_LEFT;
		}
		if(bgPB.contains(x,y)) {
			return PAUSE_START;
		}
		if(bgRA.contains(x,y)) {
			return PAUSE_RIGHT;
		}
		if(toMain.contains(x,y)) {
			return PAUSE_QUIT;
		}
		if(toSave.contains(x,y)) {
			return PAUSE_SAVE;
		}
		return -1;
	}
}
